package com.bringway.global.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.bringway.global.dao.BaseJdbcDao;
import com.bringway.global.dao.SysLogDao;
import com.bringway.global.util.StringUtils;

/*
* @author qjy
* @version 创建时间：2016年1月22日 上午9:07:52
* 类说明
*/
@Repository
public class SysLogDaoImpl extends BaseJdbcDao implements SysLogDao{
	
	Logger logger = Logger.getLogger(SysLogDaoImpl.class);

	@Override
	public void insertLoginLog(String username) {
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO sys_log(ID, USER_NAME, LOG_TYPE, LOG_DATE, LOG_CONTENT) ");
		sql.append(" VALUES (UUID(),:USER_NAME, '1', NOW(), :CONTENT) ");
		try{
			Map<String,String> paramMap = new HashMap<String,String>();
			paramMap.put("USER_NAME", username);
			paramMap.put("CONTENT", "验证登录");
			logger.info("insertLoginLog sql is : " + sql.toString());
			logger.info("insertLoginLog paramMap is : " + paramMap);
			this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
		}catch(Exception e){
			e.printStackTrace();
			this.insertExceptionLog(username, "验证登录", e);
		}
		
	}

	@Override
	public void insertExceptionLog(String username, String title,  Exception e) {
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO sys_log(ID, USER_NAME, LOG_TYPE, LOG_DATE, LOG_CONTENT) ");
		sql.append(" VALUES (UUID(),:USER_NAME, '3', NOW(), :CONTENT) ");
		try{
			Map<String,String> paramMap = new HashMap<String,String>();
			paramMap.put("USER_NAME", username);
			String content = e.getMessage();
			if(StringUtils.isNotEmpty(title)){
				content = "title : " + title + ",content : " + content;
			}
			paramMap.put("CONTENT", content);
			logger.info("insertExceptionLog sql is : " + sql.toString());
			logger.info("insertExceptionLog paramMap is : " + paramMap);
			this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}

}
